package it.uniroma2.mpsr.config;

import it.uniroma2.mpsr.resources.Centre;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class Configuration {


	private static final Configuration INSTANCE = new Configuration();
	private String[][] matrix;
	private Centre[] centres;
	private int N;


	public static Configuration getInstance()  {
		return INSTANCE;
	}

	@SuppressWarnings("unchecked")
	public void configure(String filepath) {
		Document document = null;
		SAXBuilder sb = new SAXBuilder();
		try {
			document = sb.build(new File(filepath));
		} catch (JDOMException e) {
		} catch (IOException e) {
		}
		Element root = document.getRootElement();
		N = Integer.parseInt(root.getAttributeValue("N"));
		List<Element> routingMatrix = root.getChild("routingMatrix").getChildren("line");
		matrix = new String[routingMatrix.size()][routingMatrix.size()];
		String[] line;
		for (int i=0; i < routingMatrix.size(); i++) {
			line = routingMatrix.get(i).getText().split("\\s+");
			for (int j=0; j < line.length; j++) {
				matrix[i][j] = line[j];
			}
		}
		
		List<Element> parameters = root.getChild("parameters").getChildren("centre");
		Element centre;
		centres = new Centre[matrix.length];
		for (int i=0; i<parameters.size(); i++) {
			centre = parameters.get(i);
			Centre c = new Centre(centre.getAttributeValue("kind"), Double.parseDouble(centre.getText()), Integer.parseInt(centre.getAttributeValue("mult")), centre.getAttributeValue("name"));
			centres[i] = c;
		}
	}

	public String[][] getMatrix() {
		return matrix;
	}

	public Centre[] getCentres() {
		return centres;
	}
	
	public int getN() {
		return N;
	}
	
}
